var titleText = "皇冠店";
var titleMoney = 16800;
var titleNumber = 1;

$(function () {

    $(".shop_return").tap(function(){
        location.href = httpHost + '/jiaoyi/issueIndex.html';
    });

    $(".shop_title dl").tap(function(){
        $(".artist_none").attr("artistType",0);
        titleText = $(this).find("dt").text();
        $(".shop_news ul").hide();
        $(".shop_title dl").removeClass("act");
        $(this).addClass("act");
        if(titleText == "皇冠店"){
            $(".shop_news ul").eq(1).show();
            titleNumber = 1;
            titleMoney = 16800;
        }else if(titleText == "钻石店"){
            $(".shop_news ul").eq(2).show();
            titleNumber = 2;
            titleMoney = 8400;
        }else if(titleText == "至尊店"){
            $(".shop_news ul").eq(0).show();
            titleNumber = 4;
            titleMoney = 58800;
        }else{
            $(".shop_news ul").eq(3).show();
            titleNumber = 5;
            titleMoney = 8000;
            $(".artist_none").attr("artistType",1);
        }
    });

    $(".shop_int strong span").tap(function(){
        if($(".shop_int p").css("display") == "none"){
            $(".shop_int p").show();
        }else{
            $(".shop_int p").hide();
        }
    });

    $("input[type=checkbox]").change(function(){
        if($(this).attr("checked") == true){
            $(".shop_input").css("background","#f14958");
        }else{
            $(".shop_input").css("background","#888");
        }
    });

    $(".shop_input p").tap(function(){
        if($(".shop_int input").attr("checked") == true){
            $.ajax({
                type:"get",
                url:httpHost+"/Public/isLoginInfo",
                success:function(data){
                    if(data.status == 0){
                        Ddivok1(data.message);
                        setTimeout(function(){
                            location.href = data.url;
                        },1500);
                    }else{
                        if($(".artist_none").attr("artistType") == 1){
                            $(".shop_link").hide();
                            $(".opa_background").show();
                            $(".artist_none").show();
                            var mySwiper = new Swiper('.swiper-container',{
                                pagination: '.pagination',
                                paginationClickable: true,
                                centeredSlides: true,
                                slidesPerView: 3,
                                watchActiveIndex: true,
                                onSlideChangeEnd:function(swiper){
                                    //滑动获取每一幅画的剩余量
                                    $.ajax({
                                        type:"get",
                                        url:httpHost+"/ShopApply/getShopGoodNum?good_id=" + $(".swiper-slide-active .art-show-contant").find("input").attr("typenum"),
                                        success: function(data){
                                            $(".swiper-slide-active").find(".art-show-contant em").text(data.remain_num);
                                            if(data.remain_num == 0 || $(".swiper-slide-active em").text() == 0){
                                                $(".swiper-slide-active").find("input").attr("disabled","disabled");
                                            }else{
                                                $(".swiper-slide-active").find("input").removeAttr("disabled");
                                            }
                                        },
                                        error:function(){
                                            Ddivok1("服务器连接超时，请刷新！");
                                        }
                                    });
                                }
                            });
                            $(".swiper-wrapper").css({"position":"relative","left":"-436px"});

                            //默认获取第一幅画的剩余量
                            $.ajax({
                                type:"get",
                                url:httpHost+"/ShopApply/getShopGoodNum?good_id=" + 1,
                                success: function(data){
                                    $(".swiper-slide-active").find(".art-show-contant em").text(data.remain_num);
                                    if(data.remain_num == 0 || $(".swiper-slide-active em").text() == 0){
                                        $(".swiper-slide-active").find("input").attr("disabled","disabled");
                                    }else{
                                        $(".swiper-slide-active").find("input").removeAttr("disabled");
                                    }
                                },
                                error:function(){
                                    Ddivok1("服务器连接超时，请刷新！");
                                }
                            });

                            //判断能否下一步到选择地址
                            $(".artist_button").tap(function(){
                                var slideType = 0;
                                var slideText = "";
                                var typeNum = "";
                                $(".swiper-slide").each(function(){
                                    var val = $(this).find("input").attr('checked');
                                    if(val == true){
                                        typeNum = $(this).find("input").attr('typeNum');
                                        $(this).find("p").each(function(){
                                            slideText += $(this).text();
                                        });
                                        slideType = 1;
                                        return false;
                                    }
                                });

                                //弹出地址选择
                                if(slideType == 1){
                                    $(".artist_none").hide();
                                    $(".address_none").show();

                                    //点击选择地址
                                    $(".address_big_bg dl").eq(2).tap(function(){
                                        var display_city = $(".city_show").css("display");
                                        if(display_city == "none"){
                                            $(".opa_background").show();
                                            $(".city_show").show();
                                            $(".city_show_news ul li").remove();
                                            //获取省
                                            $.ajax({
                                                type:"get",
                                                url:httpHost+"/Public/getAreaChildren?parent_id=1",
                                                success: function(data){
                                                    province(data);
                                                    $(".city_show_news li").tap(function(){
                                                        var province_id = $(this).attr("id");
                                                        $('input[name=province]').val(province_id);
                                                        var province_text = $(this).text();
                                                        //获取市
                                                        $.ajax({
                                                            type:"get",
                                                            url:httpHost+"/Public/getAreaChildren?parent_id="+ province_id +"",
                                                            success: function(data){
                                                                city(data);
                                                                $(".city_show_news li").tap(function(){
                                                                    var city_id = $(this).attr("id");
                                                                    $('input[name=city]').val(city_id);
                                                                    var city_text = $(this).text();
                                                                    //获取区
                                                                    $.ajax({
                                                                        type:"get",
                                                                        url:httpHost+"/Public/getAreaChildren?parent_id="+ city_id +"",
                                                                        success: function(data){
                                                                            district(data);

                                                                            if(data == ""){
                                                                                $(".opa_background").hide();
                                                                                $(".city_show").hide();
                                                                                $('input[name=district]').val("");
                                                                                $(".tap_city").attr("value",province_text + " " + city_text);
                                                                            }
                                                                            else{
                                                                                $(".city_show_news li").tap(function(){
                                                                                    var district_id = $(this).attr('id');
                                                                                    $('input[name=district]').val(district_id);
                                                                                    var district_text = $(this).text();
                                                                                    $(".city_show").hide();
                                                                                    $(".tap_city").attr("value",province_text + " " + city_text + " " + district_text);
                                                                                });
                                                                            }
                                                                        },
                                                                        error:function(){
                                                                            Ddivok1("服务器连接超时，请刷新！");
                                                                        }
                                                                    });
                                                                });
                                                            },
                                                            error:function(){
                                                                Ddivok1("服务器连接超时，请刷新！");
                                                            }
                                                        });
                                                    });
                                                },
                                                error:function(){
                                                    Ddivok1("服务器连接超时，请刷新！");
                                                }
                                            });
                                        }
                                    });

                                    //地址提交
                                    var address = 0;
                                    $(".address_button").tap(function(){
                                        if(address == 0){
                                            address = 1;
                                            var address_name = $(".address_name").val();
                                            var address_phone = $(".address_phone").val();
                                            var tap_city = $("input[name=city]").val();
                                            var Tap_City_Text = $(".tap_city").val();
                                            var address_area = $(".address_area").val();
                                            if(address_name == "" || address_name.length <= 0){
                                                Ddivok1("请正确填写收货人姓名");
                                            }else if(address_phone == "" || address_phone.length <= 0){
                                                Ddivok1("请正确填写联系电话");
                                            }else if(tap_city == "" || tap_city.length <= 0){
                                                Ddivok1("请选择地区");
                                            }else if(address_area == "" || address_area.length <= 0){
                                                Ddivok1("请正确填写街道、楼牌号");
                                            }else{
                                                $(".address_none").hide();
                                                $(".Ddivok2").show();
                                                $(".Ddivok2-content-info p").text("是否确认申请" + "“" + titleText + "”" + "，需扣除金额" + titleMoney + "元？").css({"line-height":"50px","margin-top":"50px"});
                                                var clickInput = 0;
                                                $("#Ddivok-true").off("tap").on("tap",function(){
                                                    if(clickInput == 0){
                                                        clickInput = 1;
                                                        $.ajax({
                                                            type:"get",
                                                            url:httpHost+"/ShopApply/doSaveApply?type=" + titleNumber + "&good_id=" + typeNum + "&good_remark=" + slideText + "&deliver_name=" + address_name + "&deliver_phone=" + address_phone + "&deliver_info=" + Tap_City_Text + address_area,
                                                            success:function(data){
                                                                if(data.status == 1){
                                                                    $(".Ddivok2").hide();
                                                                    Ddivok1(data.info);
                                                                    setTimeout(function(){
                                                                        history.go(0);
                                                                    },1500);
                                                                }else{
                                                                    clickInput = 0;
                                                                    Ddivok1(data.info);
                                                                    setTimeout(function(){
                                                                        history.go(0);
                                                                    },1500);
                                                                }
                                                            },
                                                            error:function(){
                                                                Ddivok1("服务器连接超时，请刷新！");
                                                            }
                                                        });
                                                    }
                                                });
                                                $(".Ddivok2-content-button p:first-child").tap(function(){
                                                    history.go(0);
                                                });
                                            }
                                            setTimeout(function(){address = 0;},5000);
                                        }
                                    });
                                }else{
                                    Ddivok1("请先选择签名版画");
                                }
                            });
                            //$(".artist_none b").tap(function(){
                            //    $(".artist_none").hide();
                            //    $(".opa_background").hide();
                            //    $(".shop_link").show();
                            //});
                        }else{
                            $(".Ddivok2").show();
                            $(".Ddivok2-content-info p").text("是否确认申请" + "“" + titleText + "”" + "，需扣除金额" + titleMoney + "元？").css({"line-height":"50px","margin-top":"50px"});
                            var clickInput = 0;
                            $("#Ddivok-true").off("tap").on("tap",function(){
                                if(clickInput == 0){
                                    clickInput = 1;
                                    $.ajax({
                                        type:"get",
                                        url:httpHost+"/ShopApply/doSaveApply?type=" + titleNumber,
                                        success:function(data){
                                            if(data.status == 1){
                                                $(".Ddivok2").hide();
                                                Ddivok1(data.info);
                                                setTimeout(function(){
                                                    history.go(0);
                                                },1500);
                                            }else{
                                                clickInput = 0;
                                                Ddivok1(data.info);
                                                $(".Ddivok2").hide();
                                            }
                                        },
                                        error:function(){
                                            Ddivok1("服务器连接超时，请刷新！");
                                        }
                                    });
                                }
                            });
                        }
                    }
                }
            });
        }else{
            Ddivok1("请阅读并同意开店须知");
        }
    });

    $(".shop_link").tap(function(){
        $.ajax({
            type:"get",
            url:httpHost+"/Public/isLoginInfo",
            success:function(data){
                if(data.status == 0){
                    Ddivok1(data.message);
                    setTimeout(function(){
                        location.href = data.url;
                    },1500);
                }else{
                    $.ajax({
                        type:"get",
                        url:httpHost+"/ShopApply/getShopList",
                        success:function(data){
                            if(data != null){
                                $(".shop_record").show();
                                if(data.type == 1){
                                    $(".Ddivok2-content-info ul li").eq(0).find("span").text("皇冠店");
                                }else if(data.type == 2){
                                    $(".Ddivok2-content-info ul li").eq(0).find("span").text("钻石店");
                                }else if(data.type == 4){
                                    $(".Ddivok2-content-info ul li").eq(0).find("span").text("至尊店");
                                }else{
                                    $(".Ddivok2-content-info ul li").eq(0).find("span").text("铂金店");
                                }

                                $(".Ddivok2-content-info ul li").eq(1).find("span").text(data.price);
                                $(".Ddivok2-content-info ul li").eq(2).find("span").text(data.created);
                            }else{
                                Ddivok1("暂无申请记录");
                            }
                        },
                        error:function(){
                            Ddivok1("服务器连接超时，请刷新！");
                        }
                    });
                }
            }
        });
    });

});

//省
function province(data){
    $(".city_show_news strong").text("所在省/市/自治区");
    for(var i=0; i<data.length; i++){
        var jsondata = data[i];
        var Dhtml = '<li id='+ jsondata.id +'>'+ jsondata.area +'</li>';
        $(".city_show_news ul").append(Dhtml);
    }
}

//市
function city(data){
    $(".city_show_news strong").text("所在市/盟");
    $(".city_show_news ul li").remove();
    for(var i=0; i<data.length; i++){
        var jsondata = data[i];
        var Dhtml = '<li id='+ jsondata.id +'>'+ jsondata.area +'</li>';
        $(".city_show_news ul").append(Dhtml);
    }
}

//区
function district(data){
    $(".city_show_news strong").text("所在县/区");
    $(".city_show_news ul li").remove();
    for(var i=0; i<data.length; i++){
        var jsondata = data[i];
        var Dhtml = '<li id='+ jsondata.id +'>'+ jsondata.area +'</li>';
        $(".city_show_news ul").append(Dhtml);
    }
}